
<script type="text/javascript">

//declare globals here---------------------------
var maxid=0;//global variable max id
<?php
if(!$conf->chatmode){
?>	
	var lines=8;
<?php
}
else
{
?>
	var lines=20;
<?php
}
?> 
var datalines;
var str;
var sub;
var name=""; 
var prev_message="";
var content_id=
<?php 
	if($play->piece['playlist']==0)
 		echo $play->piece['id'];
 	else
		echo $play->piece['category'];
?>;
var date = new Date();
var prev_time = null;
var prev_data="";
<?php
	if(isset($auth_session->user_data['user_id'])){
?>
var uid = <?php echo $auth_session->user_data['user_id']?>;
var ulevel = <?php echo $auth_session->user_data['user_level']?>;
<?php
}else{
	if(isset($auth_session->fb['uid'])){
?>
	var uid = <?php echo $conf->user_id ?>;
	var ulevel = <?php echo $auth_session->user_data['user_level']?>;
<?php
	}
	else
		{
			?>
		var uid = 0;
		var ulevel = 0;	   	
			<?php
		}
}
?>
//--------------Class Chat-----------------------------------------------------

function Chat()
{
	this.update = message_update;
	this.send = message_send;
	this.disableChat = disableChat;
	this.enableChat = enableChat;
	this.disconnect = chat_disconnect;
	this.showHistory = show_history;
	this.back_to_chat = back_to_chat;
	
}  

//---------------------Chat Functions------------------------------------------
	function show_history()
	{
		$.ajax({
				type: "POST",
				url: "a/process-chat.php",
				async:false,
				data: {'function':'showHistory','content_id':content_id,'ulevel':ulevel,'uid':uid},
				dataType: "json",
			success: function(data) 
			{
				
			$('#messageWrap').append(data.text);	
			
			}
		});
	}
	function back_to_chat()//update function
	{
	name = $('#nick').attr('value');
		if(name=="")
		disableChat();
		else
			enableChat(); 
			
			
	$.ajax({
				type: "POST",
				url: "a/process-chat.php",
				async:false,
				data: {'function':'update','maxid':0,'content_id':content_id,'name':name,'ulevel':ulevel,'uid':uid},
				dataType: "json",
			success: function(data) 
			{		
				maxid = data.maxid;
				$('#messageWrap').append(data.text);
				}
	
		});
	}
	
	
function chat_disconnect()
{
		$('#chat').fadeOut(250);
		$('#login').delay(500).fadeIn(250);
		
		
		



		$.ajax({
								type: "POST",
								url: "a/process-chat.php",
								async:false,
								data: {'function':'disconnect','ulevel':ulevel,'uid':uid},
								dataType: "json"
							
					});
					
}
function disableChat()
{
		
		$('#chat :input').attr('disabled', true);
		$('#nick').attr('disabled', false);
		$('#messageArea').css('overflow','hidden'); 
		$("#message").attr("value","<--  Start by entering a nickname")   
		
	
}
function enableChat()
{
	$('#messageArea').css('overflow','auto');
	$('#chat :input').attr('disabled', false);
	$('#warning_message').html(""); 
	if($("#message").attr("value")=="<--  Start by entering a nickname")
		$("#message").attr("value","");
		
		
}

	function message_send(name,msg)//send function
	{
		
		var current = new Date();
		var time_now = current.getTime();
		var time_diff;
	if(prev_time==null)
	{
			prev_time = time_now;
			time_diff = 10500;
	}
	else
	{
		time_diff = time_now - prev_time;
	}
	
	if(time_diff>=10000){
			
		if(msg!=""){    
		if(msg!=prev_message || prev_message==""){
		$("#message").attr("value","");
		$("#message").html("<img src='assets/images/loading_transparent.gif'>");
		$.ajax({
									type: "POST",
									url: "a/process-chat.php",
									data: {'function':'send','name': name, 'message': msg, 'content_id':content_id,'ulevel':ulevel,'uid':uid},
									dataType: "json",
									success:function(){
										prev_time = time_now;
										$('#message').attr('value',""); 
										$('#message').focus();
										prev_message = msg;
										}
								
									
						});
						
				
				
			
		
		}
		else
		{
			$('#messageWrap').prepend("<li><h8>repeating messages is not allowed</h8></li>");
		if(datalines>=lines)
			$("#messageWrap li:last-child").remove();  
		}
		}
	}
	else
	{
		$('#messageWrap').prepend("<li><h8>please wait 10 seconds before sending another message</h8></li>");
		if(datalines>=lines) 
			$("#messageWrap li:last-child").remove();  
	}
	
		
	}
	function message_update()//update function
	{
	name = $('#nick').attr('value');
		if(name=="")
		disableChat();
		else
			enableChat(); 
	
			
	$.ajax({
				type: "POST",
				url: "a/process-chat.php",
				async:false,
				data: {'function':'update','maxid':maxid,'content_id':content_id,'name':name,'ulevel':ulevel,'uid':uid},
				dataType: "json",
				success: function(data) 
				{
			
					
				
				if(data.nickname_exists)
				{
					alert("that nickname is already in use");
					$('#nick').attr('value',"");	
				}
				$('#login').fadeOut(250);
				$('#chat-loading').fadeOut(250);
				$('#chat').delay(500).fadeIn(250);
		
			if(data.state=='update'){
				if(prev_data!=data.text){
				if(data.diff>0 || data.initialize)
				{		
				   datalines =data.lines;
					maxid = data.maxid;
					
					$('#messageWrap').html("");					
						$('#messageWrap').prepend(data.text);		
					prev_data = data.text;
				}	
			
				
				}
					
				}
				
			   
				
			 
			}
		});
		
	}
	
	</script>
